package com.example.choupaiapp.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.util.DialogUtil;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.mine.MineEnergyActivity;
import com.gcssloop.widget.RCImageView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by ASUS on 2019/4/23.
 */

public class TransparentActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /**
         * 支持全面屏，增加了一个限制：如果是透明的Activity，
         * 则不能固定它的方向，因为它的方向其实是依赖其父Activity的（因为透明）。
         * 然而这个bug只有在8.0中有，8.1及其以后中已经修复
         */
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transparent);

        findViewById(R.id.linear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isDoubleClick()) {
                    return;
                }
                finish();
            }
        });

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String flag = bundle.getString("flag");
            switch (flag) {
                case "publish":
                    DialogUtil.getInstance().showTopDialog(this, R.layout.dialog_publish_top);
                    RelativeLayout dialog_publish_top = (RelativeLayout) DialogUtil.getInstance().dialog_top_layout;
                    TextView top_title = dialog_publish_top.findViewById(R.id.title);
                    top_title.setText("发布成功");
                    TextView msg = dialog_publish_top.findViewById(R.id.msg);
                    msg.setText("系统正在审核您的内容，当前仅自己可见");
                    RCImageView img = dialog_publish_top.findViewById(R.id.img);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (DialogUtil.getInstance().dialog_top.isShowing()) {
                                DialogUtil.getInstance().dialog_top.dismiss();
                                finish();
                            }
                        }
                    }, 2000);
                    break;

            }
        }

    }


    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }
}
